/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 22.07.2005
 *
 *************************************************/
package fightplan.views;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;

import fightplan.actions.AboutAction;
import fightplan.actions.ExitAction;
import fightplan.actions.MenuFactory;
import fightplan.actions.UndoRedoManager;
import fightplan.actions.UndoRedoManagerProvider;
import fightplan.business.Category;
import fightplan.business.Tournament;
import fightplan.business.TournamentHolder;
import fightplan.core.Resources;
import fightplan.ui.LeftMenuPanel;

@SuppressWarnings("serial")
public class MainView extends JFrame implements UndoRedoManagerProvider {
	private TournamentHolder tournament;
	private UndoRedoManager undoRedoManager;
	private CategoriesPanel categoriesPanel;

	public MainView(Tournament preselectedtournament) {
		setIconImage(Resources.getImage("goblet_gold.16.png"));
		setLayout(new BorderLayout());

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				ExitAction.exit(MainView.this);
			}
		});
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		tournament = new TournamentHolder();

		undoRedoManager = new UndoRedoManager(tournament);

		add(MenuFactory.createMenu(this), BorderLayout.NORTH);
		LeftMenuPanel leftMenuPanel = new LeftMenuPanel();
		leftMenuPanel.addCard(new TournamentInfoPanel(tournament), "Turnier", "choice_turnier.png");
		leftMenuPanel.addCard(new RegisterPanel(tournament), "Anmeldungen", "choice_anmeldungen.png");
		// leftMenuPanel.addCard(new TeamsPanel(tournament), "Teams",
		// "choice_anmeldungen.png");
		leftMenuPanel.addCard(categoriesPanel = new CategoriesPanel(tournament), "Kategorien", "choice_category.png");
		// leftMenuPanel.addCard(new DrawingPanel(tournament), "Auslosung",
		// "choice_auslosung.png");
		// leftMenuPanel.addCard(new SheetsPanel(tournament), "Austragung",
		// "choice_auslosung.png");
		// leftMenuPanel.addCard(new TimingPlan(), "Zeitplan",
		// "choice_zeitplan.png");
		// leftMenuPanel.addCard(new CategoryPanel(), "Durchführung",
		// "choice_durchfuehrung.png");

		add(leftMenuPanel, BorderLayout.CENTER);

		tournament.addValueChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				Tournament t = (Tournament) tournament.getValue();
				if (t != null && t.getMode() != null) {
					switch (t.getMode()) {
					case SKRKYU:
						setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION + " [SKR-KYU 2011]");
						break;
					case SKRKYU_KINDER:
						setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION + " [SKR-KYU Kinder 2012]");
						break;
					case IPPONSHOBU:
						setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION + " [IPPON-SHOBU 2012]");
						break;
					case FUJI2008:
						setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION + " [FUJICUP]");
						break;
					default:
						setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION);
						break;
					}
				} else {
					setTitle("Auslosungsprogramm SKR - V" + AboutAction.VERSION);
				}
			}
		});

		tournament.setValue(preselectedtournament);
	}

	public TournamentHolder getTournament() {
		return tournament;
	}

	public UndoRedoManager getUndoRedoManager() {
		return undoRedoManager;
	}

	public void putProperty(Object key, Object value) {
		((JComponent) getContentPane()).putClientProperty(key, value);
	}

	public List<Category> getSelectedCategories() {
		if (!categoriesPanel.isVisible())
			return null;

		return categoriesPanel.getSelectedCategories();
	}

}
